// pages/bar/me/feedback/feedback.js
const app = getApp()
const url = require('../../../../utils/url')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    phone: '',
    content: '',
    imgList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  setContent(e) {
    this.setData({
      content: e.detail.value
    })
  },

  setPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  addimgFun(e) {
    let that = this;
    // let imgList = [];
    let index = e.currentTarget.dataset.index;
    // console.log(index);
    let imgList = that.data.imgList;
    let imgs =  that.data.imgs;
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        // console.log(tempFilePaths, 'tempFilePaths');
        wx.uploadFile({
          url: url.customUrl + '/common/upload.do',
          filePath: tempFilePaths[0],
          name: 'files',
          success(res) {
            // console.log(res);
            if (res.data.errno > 0) {
              wx.showToast({
                title: "上传失败",
                icon: 'error'
              })
            }
            imgList[index] = JSON.parse(res.data).data[0].url;
            imgs[index] = tempFilePaths[0]
            that.setData({
              imgs: imgs,
              imgList: imgList
            })
          }
        })

        //   tempFilePaths.map((item,index) => {
        //     wx.uploadFile({
        //       url: url.customUrl + '/common/upload.do',
        //       filePath: item,
        //       name: 'files',
        //       success(res) {
        //         console.log(res);
        //         if(res.data.errno>0){
        //           wx.showToast({
        //             title: "上传失败",
        //             icon:'error'
        //           })
        //         }
        //         console.log(JSON.parse(res.data).data[0].url);
        //         imgList[index] = JSON.parse(res.data).data[0].url;
        //       }
        //     })
        //   })
        //   that.setData({
        //     imgs: res.tempFilePaths,
        //     imgList:imgList
        //   })
      }
    })
  },
  deleteimg(e) {
    let that = this;
    let curIndex = e.currentTarget.dataset.index;
    let imgs = that.data.imgs;
    imgs.splice(curIndex, 1);
    // console.log(imgs,'imgs');
    let imgList = that.data.imgList;
    imgList.splice(curIndex, 1);
    that.setData({
      imgs: imgs,
      imgList: imgList
    })
  },
  feedBack() {
    let that = this;
    // console.error(that.data.content)
    if(that.data.content==null || that.data.content=='' ){
      wx.showToast({
        title: '请填写反馈内容',
        icon:'error'
      })
      return;
    }
    wx.getUserProfile({
      desc: '需要提供用户昵称',
      success: res => {
        //console.error('授权',res);
        that.addFeedBack(res.userInfo.nickName);
      },
      fail: err => {
        // console.error('授权错误',err);
        wx.showToast({
          title: '需要记录用户昵称',
          icon: 'error'
        })
      }
    });
  },

  addFeedBack(username) {
    let that = this;
    let data = {};
    data.contact = that.data.phone;
    data.content = that.data.content;
    data.feedbackType = 0;
    data.imgList = that.data.imgList;
    data.username = username;

    // console.error("输出",data.imgList);

    app.globalData.wxRequest.request('user/addFeedback.do', data).then(res => {
      console.log('feedBack', res);
      if (res.code == '200') {
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 500);
      }else{
        wx.showToast({
          title: res.msg,
          icon:'error'
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  }
})